$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/oem/recordlist',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'recordId', index: 'recordId', width: 50 ,key:true,hidden:true},
            { label: '产品名称', name: 'loanName', index: 'loanName'},
            { label: '申请用户', name: 'username', index: 'username'},
            { label: '手机号', name: 'phone', index: 'phone'},
            // { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
            { label: '状态', name: 'status', index: 'status',formatter: function(value, options, row){
                if (value == 0)
                    return '<span class="label label-warning">待处理</span>';
                if (value == 1)
                    return '<span class="label label-primary">处理中</span>';
                if (value == 2)
                    return '<span class="label label-success">同意</span>';
                if (value == 3)
                    return '<span class="label label-danger">拒绝</span>';
                return '-';
            }},
            { label: '创建时间', name: 'createDate', index: 'createDate'},
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $("#status").change(function () {
        var v = $(this).val();
        if (v == 3)
            vm.isEdit = true;
        else
            vm.isEdit = false;
    });

});

Vue.prototype.brs = function (value) {
    return value == null?null:value.replace(/\##/g,"<br>");
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
		title: null,
		isUpdate:false,
		record: {},
        isEdit:false,
        recordStatus:0,
        user:{},
        loan:{}
	},
    filters:{
	    brs:function (value) {
            return value == null?null:value.replace(/\##/g,"<br>");
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		info:function () {
            var id = getSelectedRow();
            if(id == null)
                return ;
            vm.showList = false;
            $.getJSON("/platform/oem/recordinfo/"+id,null,function (r) {
                if (r.code == 200){
                    vm.record = r.result;
                    vm.recordStatus = r.result.status;
                    vm.userInfo(vm.record.loanUserId);
                    vm.loanInfo(vm.record.loanId);
                }
                else
                    alert(r.message);
            });
        },
        loanInfo:function (loanId) {
            $.getJSON("/platform/oem/loaninfo/"+loanId,null,function (r) {
                if (r.code == 200)
                    vm.loan = r.result;
                else
                    alert(r.message);
            })
        },
        userInfo:function (userId) {
            $.getJSON("/platform/oem/userinfo/"+userId,null,function (r) {
                if (r.code == 200)
                    vm.user = r.result;
                else
                    alert(r.message);
            })
        },
        handel: function (event) {
			var urlStr = "/platform/oem/handelrecord";
			if ($("#status").val() != 3){
			    vm.record['remake'] = "";
            }
			$.ajax({
                url:urlStr,
                type:'POST',
                contentType:"application/json",
                data:JSON.stringify(vm.record),
                success:function (r) {
                    if (r.code == 200)
                        alert("操作成功",function () {
                            vm.reload();
                        });
                    else
                        alert(r.message);
                }
            })
		},
		getInfo: function(id){
			$.get("/platform/oem/loaninfo/"+id, function(r){
				if (r.code != 200){
				    alert(r.message);
                }
                vm.loan = r.result;
				console.log("data",r);
            });
		},
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});